package com.yewen.http.repository;

import com.mongodb.client.result.UpdateResult;
import com.yewen.http.entity.EducationCounselor;
import com.yewen.http.entity.LoginAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @Classname EducationCounselorRepository
 * @Description 导生持久化类
 * @Date 2022/3/29 22:40
 * @Created by TaoVh
 */
@Repository
public class EducationCounselorRepository {
    @Autowired
    private MongoTemplate mongoTemplate;
    public boolean save(EducationCounselor educationCounselor){
        if (educationCounselor==null) return false;
        mongoTemplate.save(educationCounselor,"educounselors");
        return true;
    }
    public boolean updateState(String id,boolean state){
        Query query = new Query(Criteria.where("id").is(id));
        Update update = new Update();
        update.set("state",state);
        UpdateResult result = mongoTemplate.updateFirst(query,update,EducationCounselor.class,"educounselors");
        return true;
    }
    public List<EducationCounselor> findByMajor(String major){
        if (major.equals("")) return null;
        String pattern = ".*"+major+".*";
        Query query = new Query(Criteria.where("major").regex(pattern));
        return mongoTemplate.find(query,EducationCounselor.class,"educounselors");
    }
    public List<EducationCounselor> findByName(String name){
        if (name.equals("")) return null;
        String pattern = ".*"+name+".*";
        Query query = new Query(Criteria.where("id").regex(pattern));
        return mongoTemplate.find(query,EducationCounselor.class,"educounselors");
    }
    public List<EducationCounselor> findAll(){
        return mongoTemplate.findAll(EducationCounselor.class,"educounselors");
    }
    public List<EducationCounselor> findByTel(String tel){
        if (tel.equals("")) return null;
        String pattern = ".*"+tel+".*";
        Query query = new Query(Criteria.where("tel").regex(pattern));
        return mongoTemplate.find(query,EducationCounselor.class,"educounselors");
    }
    public List<EducationCounselor> findByEmail(String email){
        if (email.equals("")) return null;
        String pattern = ".*"+email+".*";
        Query query = new Query(Criteria.where("email").regex(pattern));
        return mongoTemplate.find(query,EducationCounselor.class,"educounselors");
    }
    public boolean loginAuth(LoginAccount loginAccount){
        Query query = new Query(Criteria.where("id").
                is(loginAccount.getId()).
                and("password").
                is(loginAccount.getPassword()));
        EducationCounselor educationCounselor = mongoTemplate.findOne(query,EducationCounselor.class,"educounselors");
        if (educationCounselor==null){
            return false;
        }else return true;
    }
}
